package c2.group.business.tb;

import c2.advanced.business.base.AdvancedDaoTemplate;
import c2.group.share.DbFacade4Group;
import cate.game.pvp.throne.data.ko.TBKoData;
import cate.game.pvp.throne.data.ps.TBPlaySide;
import cate.game.pvp.throne.data.rival.TBRivalPs;
import cate.game.pvp.throne.data.trial.TBTrialData;
import com.mongodb.client.result.DeleteResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.mongodb.core.query.Query;

import java.util.Collection;
import java.util.List;

@Slf4j
public class TBDao extends AdvancedDaoTemplate {

    public TBDao(DbFacade4Group root) {
        super(root);
    }

    //拉取对手库数据
    public List<TBRivalPs> getRivals() {
        List<TBRivalPs> list = this.mongo().findAll(TBRivalPs.class, TBRivalPs.COLLECTION);
        log.info("[王座争霸]-对手库数据拉取结束 , 共有{}个对手 ", list.size());
        return list;
    }

    //保存对手库数据
    public Collection<TBRivalPs> saveRivals(List<TBRivalPs> list) {
        DeleteResult dr = this.mongo().remove(new Query(), TBRivalPs.COLLECTION);
        Collection<TBRivalPs> r = this.mongo().insert(list, TBRivalPs.COLLECTION);
        return r;
    }


    //拉取阵容库数据
    public List<TBPlaySide> getPlySide() {
        List<TBPlaySide> list = this.mongo().findAll(TBPlaySide.class, TBPlaySide.COLLECTION);
        log.info("[王座争霸]-阵容库数据拉取结束 , 共有{}个阵容 ", list.size());
        return list;
    }

    //保存阵容库库数据
    public Collection<TBPlaySide> savePlaySide(List<TBPlaySide> list) {
        DeleteResult dr = this.mongo().remove(new Query(), TBPlaySide.COLLECTION);
        Collection<TBPlaySide> r = this.mongo().insert(list, TBPlaySide.COLLECTION);
        return r;
    }


    //拉取选拔赛数据
    public List<TBTrialData> getTrial() {
        long beginTime = System.currentTimeMillis();
        List<TBTrialData> list = this.mongo().findAll(TBTrialData.class, TBTrialData.COLLECTION);
        log.info("[王座争霸]-选拔赛数据拉取结束 ,耗时{}ms 共有{}个分组 ", (System.currentTimeMillis() - beginTime),list.size());
        return list;
    }

    //更新选拔赛数据
    public Collection<TBTrialData> saveTrial(List<TBTrialData> list) {
        DeleteResult dr = this.mongo().remove(new Query(), TBTrialData.COLLECTION);
        log.info("[王座争霸]-选拔赛更新list前先删除数据{}条, wasAcknowledged={}", dr.getDeletedCount(), dr.wasAcknowledged());
        Collection<TBTrialData> r = this.mongo().insert(list, TBTrialData.COLLECTION);
        log.info("[王座争霸]-选拔赛试图insert {} 个数据, 实际插入 {} 个数据", list.size(), r.size());
        return r;
    }


    //淘汰赛分组数据
    public List<TBKoData> getKo() {
        long beginTime = System.currentTimeMillis();
        List<TBKoData> list = this.mongo().findAll(TBKoData.class, TBKoData.COLLECTION);
        log.info("[王座争霸]-淘汰赛数据拉取结束 ,耗时{}ms 共有{}个分组 ", (System.currentTimeMillis() - beginTime),list.size());
        return list;
    }

    //更新淘汰赛分组数据
    public Collection<TBKoData> saveKo(List<TBKoData> list) {
        DeleteResult dr = this.mongo().remove(new Query(), TBKoData.COLLECTION);
        log.info("[王座争霸]-淘汰赛更新list前先删除数据{}条, wasAcknowledged={}", dr.getDeletedCount(), dr.wasAcknowledged());
        Collection<TBKoData> r = this.mongo().insert(list, TBKoData.COLLECTION);
        log.info("[王座争霸]-淘汰赛试图insert {} 个数据, 实际插入 {} 个数据", list.size(), r.size());
        return r;
    }



}
